package com.iot.inclinometer.ui;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.iot.inclinometer.R;
import com.iot.inclinometer.util.MyAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * "选择.xls文件"
 *
 * @author WXR
 */
public class MyXlsManager extends ListActivity {
    private List<String> items = null;
    private List<String> paths = null;
    private String rootPath = getSDPath(MyXlsManager.this);
    private String curPath = "/";
    private String filePath = "/";
    private TextView mPath;

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.xls_select);
        mPath = (TextView) findViewById(R.id.mPath);
        Button buttonCancle = (Button) findViewById(R.id.buttonCancle);
        buttonCancle.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                finish();
            }
        });
        getFileDir(rootPath);
    }

    private void getFileDir(String filePath) {
        mPath.setText(filePath);
        items = new ArrayList<String>();
        paths = new ArrayList<String>();
        File f = new File(filePath);
        File[] files = f.listFiles();

        if (null != files) {

            if (!filePath.equals(rootPath)) {
                items.add("b1");
                paths.add(rootPath);
                items.add("b2");
                paths.add(f.getParent());
            }
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                items.add(file.getName());
                paths.add(file.getPath());
            }
        }
        setListAdapter(new MyAdapter(this, items, paths));
    }

    private String getSDPath(Context context) {
        String sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory().getAbsolutePath();// 获取根目录
        } else {// 提示没检测到SD卡
            Toast.makeText(context, "没检测到SD卡！", Toast.LENGTH_SHORT).show();
            sdDir = "/";
        }
        return sdDir;
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        File file = new File(paths.get(position));
        if (file.isDirectory()) {
            curPath = paths.get(position);
            getFileDir(paths.get(position));
        } else {
            filePath = file.getPath();
            String fName = file.getName();
            String end = fName
                    .substring(fName.lastIndexOf(".") + 1, fName.length()).toLowerCase();
            if (end.equals("xls")) {
                Intent data = new Intent(MyXlsManager.this, OutputProUI.class);
                Bundle bundle = new Bundle();
                bundle.putString("file", filePath);
                data.putExtras(bundle);
                setResult(2, data);
                finish();
            } else {
                Toast.makeText(MyXlsManager.this, "请选择或输入以.xls为后缀的文件！",
                        Toast.LENGTH_LONG).show();
            }
        }
    }
}